package com.huowolf.oa.view.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.huowolf.oa.domain.Department;
import com.huowolf.oa.domain.Role;
import com.huowolf.oa.service.DepartmentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class DepartmentAction extends ActionSupport implements ModelDriven<Department>{
	
	@Resource
	private DepartmentService departmentService;
	
	private Department departmentModel = new Department();
	
	@Override
	public Department getModel() {
		return departmentModel;
	}
	
	/*** 列表  */
	public String list() throws Exception {
		List<Department> departmentList = departmentService.findAll();
		ActionContext.getContext().put("departmentList", departmentList);
		return "list";
	}
	
	/*** 删除 */
	public String delete() throws Exception {
		departmentService.delete(departmentModel.getId());
		return "toList";
	}
	
	/*** 添加页面 */
	public String addUI() throws Exception {
		return "saveUI";
	}
	
	/*** 添加 */
	public String add() throws Exception {
		departmentService.save(departmentModel);
		return "toList";
	}
	
	/*** 修改页面 */
	public String editUI() throws Exception {
		Department department = departmentService.getById(departmentModel.getId());
		ActionContext.getContext().getValueStack().push(department);
		return "saveUI";
	}
	
	/*** 修改 */
	public String edit() throws Exception {
		//1.从数据库中获取原对象
		Department department = departmentService.getById(departmentModel.getId());
		//2.设置要修改的属性
		department.setName(departmentModel.getName());
		department.setDescription(departmentModel.getDescription());
		//3.更新到数据库
		departmentService.update(department);
		return "toList";
		
	}


}
